package top.mczhengyi.xyblog.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.mczhengyi.xyblog.auth.RequireLogin;
import top.mczhengyi.xyblog.entity.Reply;
import top.mczhengyi.xyblog.entity.Result;
import top.mczhengyi.xyblog.entity.ResultCode;
import top.mczhengyi.xyblog.mapper.ReplyMapper;
import top.mczhengyi.xyblog.service.ReplyService;

import java.util.Objects;

@RestController
@RequestMapping("/blog/reply")
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    @GetMapping("/{id}")
    public Result getBlogReply(@PathVariable("id") int id) {
        return Result.success(replyService.getRepliesByArticleId(id));
    }

    @GetMapping("/all")
    @RequireLogin
    public Result getAllBlogReply() {
        return Result.success(replyService.getAllReply());
    }

    @PostMapping("")
    public Result saveReply(Reply reply) {
        if ("".equals(reply.getUsername()) || "".equals(reply.getEmail()) || "".equals(reply.getContent())) {
            return Result.fail(ResultCode.REQUIRE_PARAM);
        }
        Integer result = replyService.saveReply(reply);
        if (result == 0) {
            return Result.fail(ResultCode.UNKNOWN_FAIL);
        }
        return Result.success(reply.getReplyId());
    }

    @PostMapping("/apply/{id}")
    @RequireLogin
    public Result applyReply(@PathVariable(value = "id", required = true) int id) {
        if (replyService.applyReply(id) == 0) {
            return Result.fail(ResultCode.UNKNOWN_FAIL);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @RequireLogin
    public Result deleteReply(@PathVariable("id") int id) {
        if (replyService.deleteReply(id) == 0) {
            return Result.fail(ResultCode.UNKNOWN_FAIL);
        }
        return Result.success();
    }
}
